<%@ WebHandler Language="C#" Class="webapp4U.Img" %>

// Copyright (C) 2005 by Greg Ennis
// (mailto:webapp4u@yahoo.com)
// The contents of this file are subject to the Artistic License (the "License").
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at:
// http://www.webapp4u.net


using System;
using System.Web;
using System.IO;
using System.Drawing;
using webapp4U.BOL;

namespace webapp4U
{
	public class Img : IHttpHandler 
	{ 
		public bool IsReusable 
		{ get { return true; } } 

		public void ProcessRequest(HttpContext Context) 
		{
            // Writing an image to output stream
            Context.Response.ContentType = "image/jpg";
                        
            string FilePath = "";
            string Action = Globals.GetStringFromQueryString("Action").Trim();
            
            if(Action.ToLower()=="logo")
                FilePath = Globals.PhysicalApplicationPath() + "UserUpload/Logo/" + Globals.GetStringFromQueryString("ImageUrl");    
            else
                FilePath = Globals.PhysicalApplicationPath() + "UserUpload/Bosuutap/"+ Globals.GetStringFromQueryString("ImageUrl");
            
			// 'thumbnail' means we are requesting a thumbnail
            if (Context.Request["Size"] != null)
			{
                float size = 100.0f ;
                switch (Context.Request.QueryString["Size"])
                {
                    case "S":
                        size = 40.0f;
                        break;
                    case "M":
                        size = 100.0f;
                        break;
                    case "M2":
                        size = 250.0f;
                        break;
                    case "L":
                        size = 400.0f;
                        break;
                    case "L2":
                        size = 500.0f;
                        break;
                }
                
                //float thumbnail = float.Parse(Context.Request["thumbnail"]);
				// Need to load the image, resize it, and stream to the client.
				// Calculate the scale so as not to stretch or distort the image.
				Bitmap bmp = new Bitmap(FilePath);
                if (bmp.Width > size || bmp.Height > size)
                {
                    float scale = size / System.Math.Max(bmp.Height, bmp.Width);
                    System.Drawing.Image thumb = bmp.GetThumbnailImage((int)(bmp.Width * scale), (int)(bmp.Height * scale), null, System.IntPtr.Zero);
                    thumb.Save(Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    bmp.Dispose();
                    thumb.Dispose();
                }
                else
                    ShowImage(FilePath);
			}
			else
			{
                ShowImage(FilePath);
			}
		}
        
        public void ShowImage(string FilePath)
        {
            // Stream directly from the file
            // Get the stream and send it out the response
            System.IO.FileStream fs = System.IO.File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            const int byteLength = 1024 * 16;
            byte[] bytes = new byte[byteLength];
            while (fs.Read(bytes, 0, byteLength) != 0)
            {
                HttpContext.Current.Response.BinaryWrite(bytes);
            }
            fs.Close();
        }
	}
}


